package ddd.ajava.chapter2.question4;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

/**
 * @author lenovo
 * 代理步骤：
 * 1.计算时间
 * 2.获得方法名称
 * 3.取得参数
 */
public class PersonProxy implements SetInterface{
	private Person person;
	private StringBuilder sb;
	
	public PersonProxy(Person person){
		this.person=person;
	}
	
	@Override
	public void setIdNo(String idNo) {
		sb=new StringBuilder();
		this.person.setIdNo(idNo);
		this.calTime();
		sb.append("方法名称：setIdNo;");
		sb.append("参数："+idNo+"\n");
		this.outputToTxt();
	}

	@Override
	public void setName(String name) {
		sb=new StringBuilder();
		this.person.setName(name);
		this.calTime();
		sb.append("方法名称：setName;");
		sb.append("参数："+name+"\n");
		this.outputToTxt();
	}

	@Override
	public void setAge(Integer age) {
		sb=new StringBuilder();
		this.person.setAge(age);
		this.calTime();
		sb.append("方法名称：setAge;");
		sb.append("参数："+age+"\n");
		this.outputToTxt();
	}

	@Override
	public void setSex(String sex) {
		sb=new StringBuilder();
		this.person.setSex(sex);
		this.calTime();
		sb.append("方法名称：setSex;");
		sb.append("参数："+sex+"\n");
		this.outputToTxt();
	}

	@Override
	public void setIsMerried(Boolean isMerried) {
		sb=new StringBuilder();
		this.person.setIsMerried(isMerried);
		this.calTime();
		sb.append("方法名称:setIsMerried;");
		sb.append("参数:"+isMerried+"\n");
		this.outputToTxt();
	}
	
	private void outputToTxt(){
		String path=this.getClass().getPackage().getName().replace(".","\\");
		String relativePath="src\\"+path+"\\"+this.getClass().getSimpleName()+".txt";
		try{
			BufferedWriter bw=new BufferedWriter(new FileWriter(relativePath,true));
			bw.write(sb.toString());
			bw.flush();
			bw.close();
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}
	
	private  void calTime(){
		Calendar time=Calendar.getInstance();
		int year=time.get(Calendar.YEAR);
		int month=time.get(Calendar.MONTH)+1;
		int day=time.get(Calendar.DAY_OF_MONTH);
		int hour=time.get(Calendar.HOUR_OF_DAY);
		int min=time.get(Calendar.MINUTE);
		int second=time.get(Calendar.HOUR_OF_DAY);
		String s="时间:"+year+"-"+month+"-"+day+" "+hour+":"+min+":"+second+";";
		sb.append(s);
	}

}
